/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package GuiTools;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import Settings.Settings;

/**
 * A panel containing a LabelPanel with (depending on the given argument) either a JComboBox or a TextField.
 * 
 * @author Christoph Schnack
 *
 */
public class MetaElement extends JPanel{
	boolean textfield = false;
	String name;
	public TribuitTextField tf;
	JComboBox cb;
	Settings SET;
	ActionListener tfListener;
	
	public MetaElement(Settings set,String n, Vector<String> val ,ActionListener al){
		this(set,n,val);
		if(textfield) tf.addActionListener(al);
	}
	/**
	 * 
	 * @param set SETTINGS
	 * @param n Name of the meta information
	 * @param val either a vector or null
	 */
	public MetaElement(Settings set,String n, Vector<String> val ){
		super();
		SET=set;
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		name = n;
		if(val == null){
			textfield = true;
			tf = new TribuitTextField(set,10);
			LabelPanel t2LP = new LabelPanel(name+":",tf);
			t2LP.setOpaque(false);
			this.add(t2LP,gbc);
		}
		else{
			cb = new JComboBox(val);
			LabelPanel t2LP = new LabelPanel(name+":",cb);
			t2LP.setOpaque(false);
			this.add(t2LP,gbc);
		}
		this.setOpaque(false);
	}
	
	public boolean isTextfield(){
		return textfield;
	}
	
	public void setError(){
		if(textfield) tf.setBorder(BorderFactory.createLineBorder(Color.red,2));
	}
	
	public void clearBorder(){
		if(textfield) tf=new TribuitTextField(SET,tf.getText(),10);
	}
	
	public void mark(){
		if(textfield){
			tf.selectAll();
			tf.requestFocus();
		}
		else{
			cb.requestFocus();
		}
	}
	
	public void reset(){
		if(textfield)tf.setText("");
		else cb.setSelectedIndex(0);
	}
	
	public String getMetaName(){
		return name;
	}
	
	public JComponent getInputComponent(){
		return textfield?tf:cb;
	}
	
	public String getValue(){
		if(textfield) return tf.getText();
		else return (String)cb.getSelectedItem();
	}
	
	/**
	 * Sets either the text field or the combo box the given value. In case of a combo box an empty String argument sets it's selected index to zero.
	 * @param txt text
	 */
	public void setValue(String txt){
		if(textfield) tf.setText(txt);
		else{
			if(txt.equals("")) cb.setSelectedIndex(0);
			else cb.setSelectedItem(txt);
		}
	}
}